﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace BesAsm.Emgaats.Core.Commands
{
  public class MultiCommand : SingleCommand
  {
    private List<IEmgaatsExtensionCommand> _commands;

    /// <summary>
    /// Creates an empty multicommand
    /// </summary>
    public MultiCommand()
    {
      _commands = new List<IEmgaatsExtensionCommand>();
    }

    public MultiCommand(List<IEmgaatsExtensionCommand> commandList)
    {
      _commands = commandList;
    }

    public void AddCommand(IEmgaatsExtensionCommand command)
    {
      _commands.Add(command);
    }

    #region IEmgaatsExtensionCommand Members

    public override void Execute()
    {
      base.Execute();
      var q = from c in _commands
              orderby _commands.IndexOf(c)
              select c;
      foreach (var c in q)
        c.Execute();
    }

    public override void Undo()
    {
      var q = from c in _commands
              orderby _commands.IndexOf(c) descending
              select c;
      foreach (var c in q)
        c.Undo();
    }

    public override string Name
    {
      get { return "Multiple commands"; }
    }

    public override string Description
    {
      get { return "Executes multiple commands"; }
    }

    #endregion
  }
}
